package com.yanliang.serializer;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

/**
 * @author yanliang
 * @date 5/23/20203:13 PM
 */
public class RpcDecoder extends ByteToMessageDecoder {
	private Class<?> clazz;

	private Serializer serializer;

	public RpcDecoder(Class<?> clazz, Serializer serializer) {
		this.clazz = clazz;
		this.serializer = serializer;
	}


	protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
		int dataLength = byteBuf.readInt();
		byte[] body = new byte[dataLength];
		byteBuf.readBytes(body);  //
		Object obj = serializer.deserialize(clazz, body);
		list.add(obj);
	}
}
